/*
 *
 *  Copyright (C) 2008-2011 GSyC/LibreSoft, Universidad Rey Juan Carlos
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Roberto Calvo Palomino <rocapal@libresoft.es>
 *
 */
package com.sitem.appsjam.Overlays;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sitem.appsjam.R;
import com.sitem.appsjam.Types.ArPunt;
import com.sitem.appsjam.Types.Punt;
import com.sitem.appsjam.Utils.LocationUtils;

public class ARSummaryBox{
	
	private Activity mActivity;
	private RelativeLayout container;
	private View summary_box;
	
	private int num_node = -1;
	private ArrayList<ArPunt> nodes_list = null;
	private ImageView[] image_containers;
	
	//private AudioPlayer media_player;
	
	public ARSummaryBox(Activity mActivity, RelativeLayout container){
		this.container = container;
		this.mActivity = mActivity;
	}
	
	public void drawSummaryBox(final Activity mContext, final int num_node){
		setNodeClicked(num_node);
		if(num_node == -1) return;
		ArPunt arnode = getNode(num_node);
		if(arnode == null) return;
		final Punt p = arnode.getPunt();
		
		View summary_box = getSummaryBox();
		if(summary_box == null){
			LayoutInflater factory = LayoutInflater.from(mContext);
			summary_box = factory.inflate(R.layout.ar_simple_summary, null);
			
			int h = mActivity.getWindowManager().getDefaultDisplay().getHeight();
			summary_box.setPadding(60, h*7/12, 60, 0);
			
			setSummaryBox(summary_box);
			setCloseButton(summary_box);
		}


		TextView t = (TextView) mActivity.findViewById(R.id.ar_static_title);
		t.setText(p.getName());

		t = (TextView) mActivity.findViewById(R.id.ar_static_layer);
		t.setText(mActivity.getString(R.string.layer) + " " + p.getLayer().getName(mActivity.getBaseContext()));

		t = (TextView) mActivity.findViewById(R.id.ar_static_distance);
		t.setText(" "+LocationUtils.displayDistance(arnode.getDistance(), LocationUtils.AUTO));

		t = (TextView) mActivity.findViewById(R.id.ar_static_description);
		t.setText(p.getDescription());

		ImageView img = (ImageView) mActivity.findViewById(R.id.ar_image);
		setImageContainer(num_node, img);
		//img.setImageBitmap(arnode.getImage(true));
		img.setImageBitmap(arnode.getImageThumb(true)); //Mateixa imatge que a la miniatura
			
		//Link a la fitxa
		img.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String url =  p.getUrlInfo();
				Intent i = new Intent("android.intent.action.VIEW", android.net.Uri.parse(url));
				mContext.startActivity(i);
			}
        });	
	}
	
	protected RelativeLayout getContainer(){
		return container;
	}
	
	public void setNodesList(ArrayList<ArPunt> nodes_list){
		this.nodes_list = nodes_list;
		if(nodes_list != null) image_containers = new ImageView[nodes_list.size()];
	}
	
	public ArrayList<ArPunt> getNodesList(){
		return nodes_list;
	}
	
	public void setNodeClicked(int num_node){
		this.num_node = num_node;
	}
	
	public int getNodeClicked(){
		return num_node;
	}
	
	public ArPunt getNode(){
		return getNode(num_node);
	}
	
	public ArPunt getNode(int num_node){
		if((nodes_list != null) && (num_node > -1)) return nodes_list.get(num_node);
		return null;
	}
	
	protected void setSummaryBox(View summary_box){
		this.summary_box = summary_box;
		Log.i("setSummaryBox",""+isBoxDrawn());
		if(!isBoxDrawn()) {
			container.addView(this.summary_box, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
			container.invalidate();
		} else {
			container.invalidate();
		}
	}
	
	protected View getSummaryBox(){
		return summary_box;
	}
	
	protected void showBox(){
		if(summary_box != null)	summary_box.setVisibility(View.VISIBLE);
	}
	
	protected void hideBox(){
		if(summary_box != null)	summary_box.setVisibility(View.INVISIBLE);
	}
	
	public void removeSummaryBox(){
		container.removeView(summary_box);
		summary_box = null;
		num_node = -1;
	}
	
	protected void setImageContainer(int position, ImageView image_container){
		if(position > -1) image_containers[position] = image_container;
	}

	public void resetAll(){
		removeSummaryBox();
	}
	
	protected boolean isBoxDrawn(){
		return (container.indexOfChild(summary_box) > -1);
	}
	
	public void refreshPhoto(int num_node, Bitmap image){
		if((image_containers[num_node] == null)) return;
		
		image_containers[num_node].setImageBitmap(image);
//		image_container.invalidate();
	}
	
	protected void setCloseButton(View summary_box){
		if(num_node == -1) return;
		final ArPunt arnode = getNode();

		if(arnode == null) return;
		
		OnClickListener listener = new OnClickListener() {
			public void onClick(View v) {
				//if((arnode.isMedia()) && media_player != null) media_player.stop();
				arnode.setClicked(false);
			}
		};
		
		Button bt = (Button)summary_box.findViewById(R.id.ar_button_close);
		bt.setOnClickListener(listener);
	}
/*	
	protected void setPlayButton(Context mContext, final int num_node, View summary_box){
		if(num_node == -1) return;
		ArPunt arnode = getNode(num_node);

		if((arnode == null) || (!arnode.isMedia())) return;

		if(media_player == null) media_player = new AudioPlayer(mContext);

		Button bt = (Button)summary_box.findViewById(R.id.ar_button_play);
		bt.setOnClickListener(media_player.getPlayButtonListener((Audio)arnode.getGeoNode()));
	}
	
	protected void setStopButton(View summary_box){
		if(media_player != null)
			media_player.stop();

		Button bt = (Button)summary_box.findViewById(R.id.ar_button_stop);
		bt.setOnClickListener(media_player.getStopButtonListener());
	}
	*/	
}